package cn.yikaojiuguo.student.ui.ask;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.media.AudioFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.support.utils.ToastUtil;
import cn.yikaojiuguo.student.support.utils.Util;
import cn.yikaojiuguo.student.ui.base.CustomerActionBarActivity;
import cn.yikaojiuguo.student.ui.view.RecordStateView;
import cn.yikaojiuguo.student.ui.view.TimeScaleView;
import cn.yikaojiuguo.student.ui.view.TimerView;

public class AskRecordActivity extends CustomerActionBarActivity implements OnClickListener {

	private boolean isFirst = true;
	private TimerView timerView;
	private TimeScaleView tsv;
	private RecordStateView rsv;
	private SurfaceView sfv;
	private RecordAndDraw recordAndDraw;
	// 采样频率
	private static final int frequency = 8000;
	// 单声道
	private static final int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
	// 音频数据格式：每个样本16位
	private static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
	private RecordAndDraw.OnFinishRecordListener listener = new RecordAndDraw.OnFinishRecordListener() {

		@Override
		public void onFinishRecord(File file) {
			if (file != null) {
				System.out.println(file.getName());
				file.delete();
			}
		}
	};
	OnClickListener cancelClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (showDialog != null) {
				showDialog.dismiss();
			}
		}
	};
	OnClickListener confirmToDeleteClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (recordAndDraw.amrFile == null) {
				return;
			}
			if (!recordAndDraw.amrFile.delete()) {
				recordAndDraw.setOnRecordFinishListener(listener);
			}
			timerView.clear();
			rsv.setState(RecordStateView.PRE_RECORD);
			tsv.setVisibility(View.INVISIBLE);
			recordAndDraw.clear();
			playPause = false;
			if (showDialog != null) {
				showDialog.dismiss();
			}
			v_delete.setVisibility(View.GONE);
		}
	};
	OnClickListener confirmToDeleteClickListener2 = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (recordAndDraw.amrFile == null) {
				return;
			}
			if (!recordAndDraw.amrFile.delete()) {
				recordAndDraw.setOnRecordFinishListener(listener);
			}
			timerView.clear();
			rsv.setState(RecordStateView.IS_RECORDING);
			tsv.setVisibility(View.VISIBLE);
			tsv.start();
			timerView.setVisibility(View.VISIBLE);
			recordAndDraw.clear();
			recordAndDraw.start();
			playPause = false;
			if (showDialog != null) {
				showDialog.dismiss();
			}
			v_delete.setVisibility(View.GONE);
		}
	};
	private List<OnClickListener> listenerList = new ArrayList<OnClickListener>();
	private List<OnClickListener> listenerList2 = new ArrayList<OnClickListener>();
	private Dialog showDialog;
	private Dialog deleteDialog;
	private Dialog deleteDialog2;
	private boolean playPause = false;
	private View v_delete;

	public static Intent newIntent() {
		Intent intent = new Intent(YiKaoApplication.getInstance(),AskRecordActivity.class);
		return intent;
	}

	@Override
	protected void onCreate(Bundle arg0) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager
				.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(arg0);
		setContentView(R.layout.activity_ask_record);
		setCustomerTitle("");
		setDividerVisibility(false);
		setCustomerBackgroundColor(0xff464646);
		listenerList.add(cancelClickListener);
		listenerList.add(confirmToDeleteClickListener);
		listenerList2.add(cancelClickListener);
		listenerList2.add(confirmToDeleteClickListener2);
		deleteDialog = Util.getCustomerDialog(this, "确认删除", "确认后将删除该录音", new String[]{"取消", "确认"},
				listenerList);
		deleteDialog2 = Util.getCustomerDialog(this, "确认删除", "继续录音将删除之前的录音", new String[] { "取消", "确认" },
				listenerList2);
		timerView = (TimerView) findViewById(R.id.tv_timer);
		tsv = (TimeScaleView) findViewById(R.id.tsv);
		rsv = (RecordStateView) findViewById(R.id.rsv);
		sfv = (SurfaceView) findViewById(R.id.sfv);
		v_delete = findViewById(R.id.v_delete);
		timerView.setOnMaxTimeListener(new TimerView.onMaxTimeListener() {

			@Override
			public void timeOut() {
				rsv.setState(RecordStateView.STOP);
				recordAndDraw.stop();
				ToastUtil.showToast(AskRecordActivity.this, "作品录制时间已满");
			}
		}, 120000);
		sfv.setZOrderOnTop(true);
		sfv.getHolder().setFormat(PixelFormat.TRANSLUCENT);
		rsv.ib_start_record.setOnClickListener(this);
		rsv.ib_pause_record.setOnClickListener(this);
		rsv.ib_play.setOnClickListener(this);
		rsv.ib_play_pause.setOnClickListener(this);
		rsv.ib_ok.setOnClickListener(this);
		v_delete.setOnClickListener(this);
		recordAndDraw = new RecordAndDraw(this, frequency, channelConfiguration, audioEncoding, sfv, timerView);
		recordAndDraw.player.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				playPause = false;
				recordAndDraw.endIndex = 0;
				recordAndDraw.redrawTimer.cancel();
				recordAndDraw.redrawBuf.clear();
				rsv.setState(RecordStateView.STOP);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_start_record:
			if (rsv.getState() == RecordStateView.PRE_RECORD) {
				tsv.setVisibility(View.VISIBLE);
				tsv.start();
				rsv.setState(RecordStateView.IS_RECORDING);
				recordAndDraw.start();
				v_delete.setVisibility(View.VISIBLE);
			} else {
				v_delete.setVisibility(View.VISIBLE);
				showDialog = deleteDialog2;
				deleteDialog2.show();
			}
			break;
		case R.id.iv_pause_record:
			v_delete.setVisibility(View.VISIBLE);
			timerView.stop();
			tsv.stop();
			tsv.setVisibility(View.INVISIBLE);
			rsv.setState(RecordStateView.STOP);
			recordAndDraw.stop();
			break;
		case R.id.iv_play:
			tsv.setVisibility(View.INVISIBLE);
			rsv.setState(RecordStateView.IS_PLAYING);
			if (!playPause) {
				recordAndDraw.playAndDraw();
			} else {
				// 继续播放
				recordAndDraw.playStart();
			}
			break;
		case R.id.iv_play_pause:
			tsv.setVisibility(View.VISIBLE);
			rsv.setState(RecordStateView.STOP);
			recordAndDraw.playPause();
			playPause = true;
			break;
		case R.id.v_delete:
			showDialog = deleteDialog;
			deleteDialog.show();
			break;
		case R.id.iv_ok:
			if (timerView.getMilliSecondTime() < 15000) {
				ToastUtil.showToast(this, "作品过短，请至少录制15秒钟的作品");
				return;
			}
			recordAndDraw.clear();
			startActivity(AskActivity.newRecordIntent(timerView.getFixedTime(),timerView.getSecondTime() + ""));
			finish();
			break;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		recordAndDraw.release();
	}

	@Override
	protected void onResume() {
		System.out.println("onResume");
		super.onResume();
		if (rsv.getState() == RecordStateView.STOP) {
			recordAndDraw.drawBackUp();
		}
		if (isFirst) {
			isFirst = false;
			if (recordAndDraw != null) {
				if (!recordAndDraw.checkPermission()) {
					ToastUtil.showToast(this, "录音权限被禁止");
					finish();
				}
			}
		}

	}
}
